/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package me.everdras.blackPlague;

import org.bukkit.entity.Player;
import java.util.LinkedList;
import java.util.List;
import org.bukkit.entity.Entity;

/**
 *
 * @author Josh
 */
public class PlagueThread extends Thread {
    private final LinkedList<Player> infectedPlayers, toBeInfected, toBeRemoved;
    
    
    private double lastTick;
    private boolean cancelled;
    
    
    
    public PlagueThread() {
        infectedPlayers = new LinkedList<Player>();
        toBeInfected = new LinkedList<Player>();
        toBeRemoved = new LinkedList<Player>();
        lastTick = System.currentTimeMillis();
        cancelled = false;
        
        
        
    }
    
    @Override
    public void run() {
        while(!cancelled) {
            
            synchronized(infectedPlayers) {
                for(Player p : infectedPlayers) {

                    synchronized(p) { //Should I bother getting the intrinsic lock of the Player for this?
                        p.damage(4); //i assume this is doing 2 hearts of damage
                        p.sendMessage(org.bukkit.ChatColor.DARK_BLUE + "You were damaged by the Plague!");

                        if(p.getHealth() < 0) {
                            toBeRemoved.add(p);
                        }
                    }

                    List<Entity> near = p.getNearbyEntities(5, 5, 5);

                    for(Entity e : near) {
                        if(e instanceof Player) {
                            toBeInfected.add((Player) e);
                        }
                        ((Player) e).sendMessage(org.bukkit.ChatColor.DARK_BLUE + "You were infected by " + ((Player) e).getName());
                    }
                }

                //add players that are newly infected to the list
                for(Player addme : toBeInfected) {

                    infectedPlayers.add(addme);

                }
                toBeInfected.clear();

                //remove players who are dead/cured from the list
                for(Player removeme : toBeRemoved) {
                    infectedPlayers.remove(removeme);
                }
                toBeRemoved.clear();
            } //end of lock on infectedPlayers
            
            lastTick = System.currentTimeMillis();
            
            
            while(  (System.currentTimeMillis() - lastTick)  < 60000) {}
            
            
            
        }//end while(!cancelled)
    }
    
    
    public void addInfected(Player infected) {
        
        
        synchronized(infectedPlayers) {
            infectedPlayers.add(infected);
        }
    }
    
    
    public void cure(Player cureme) {
        
        
        synchronized(infectedPlayers) {
            infectedPlayers.remove(cureme);
        }
    }
    
    
    
    public void cureAll() {
        synchronized(infectedPlayers) {
            infectedPlayers.clear();
        }
    }
    
    public void terminate() {
        cancelled = true;
    }
    
    
    
}
